import Vue from 'vue'
import Vuex from 'vuex'
import citysJSON from '@/vuex/citysJSON';
import classificationJSON from '@/vuex/classificationData';
import util from '@/util/util';
import * as allinpay from '../allinpayProperties';

Vue.use(Vuex)

const state = {
    //common  json   begin
    citysJSON:citysJSON,
    longitude:0,//经度
    latitude:0,//纬度
    //common  json   end

    etsUrl:'',
    imgBaseUrl:'',//配置测试或生产的图片服务器地址
    HotMap:[],//热门商户
    NavigationMap:[],//导航数据
    ADVLST:[],//轮播图的数据

    yanQianParam:'',
    yanQian:{},

    classificationMap:classificationJSON//联动的数据
}

const mutations = {
    setJingWei2(state,param){
        state.longitude = param.longitude;//经度
        state.latitude = param.latitude;//纬度
    },

    setYanQianParam(state,param){
        sessionStorage.setItem('yanQianParam',param);//请求的时候的验签参数
        //state.yanQianParam = param;
    },
    setYanQian(state,param){
        sessionStorage.setItem('yanQianId',param);//验签数据
        //state.yanQian = param;//验签数据
    },
    //记录选中的城市
    setSelectedCity(state,param){
        state.citysJSON.selected = param;
    },
    /*setIndexData(state,param){
        state.HotMap = param.HotMap;
        state.NavigationMap = param.NavigationMap;
    },*/
    setHotMap(state,param){
        state.HotMap = param.HotMap;
    },
    setNavigationMap(state,param){
        state.NavigationMap = param.NavigationMap;
    },
    setADVLST(state,param){
        state.ADVLST = param.ADVLST;
    },
    setImgBaseUrl(state,param){
        if('development'===param){
            //state.imgBaseUrl = 'http://192.168.12.71:1006/mmpimg'//测试环境图片地址
            //state.etsUrl = 'http://test.allinpay.com';//测试环境ip
            state.etsUrl = allinpay.development.etsUrl;
            //state.imgBaseUrl = allinpay.development.imgBaseUrl;
        }else{
            //state.imgBaseUrl = 'http://192.168.12.71:1006/mmpimg';//生产环境图片地址
            //state.etsUrl = 'http://test.allinpay.com';//生产环境ip

            state.etsUrl = allinpay.production.etsUrl;
            //state.imgBaseUrl = allinpay.production.imgBaseUrl;
        }
    },
    //设置联动的数据
    setClassificationData(state,param){
        if(param&&param.SQLST){
            state.classificationMap.NEAR[1].categorylast = new Array();
            param.SQLST.forEach((obj,i)=>{
                var temp = {categoryNumber: obj.circleNumber, categoryName: obj.circleName};
                state.classificationMap.NEAR[1].categorylast.push(temp);//设置联动选择然后展示右边的数据，       左边的附近、区域、热门商区  是写死的。可以去classificationData.js文件查看数据结构
            });

        }
        if(param&&param.AREALST){
            state.classificationMap.NEAR[2].categorylast = new Array();
            param.AREALST.forEach((obj,i)=>{
                var temp = {categoryNumber: obj.subdomainNumber, categoryName: obj.subdomainName};
                state.classificationMap.NEAR[2].categorylast.push(temp);
            });
        }
        if(param&&param.TAGLST){
            state.classificationMap.TAGLST = param.TAGLST;
        }
    }
}

const actions = {

    //获取经纬
    jingWeiData(store,data){
        return new Promise((resolve,reject)=>{
            if(undefined!=window.allinpaywallet&&undefined!=window.allinpaywallet.awLatAndLng ()&&''!=window.allinpaywallet.awLatAndLng ()){
                var latLng = JSON.parse(window.allinpaywallet.awLatAndLng ());
                //sessionStorage.setItem('longitude',latLng.lng);//经度
                //sessionStorage.setItem('latitude',latLng.lat);//纬度
                let points = {longitude:latLng.lng,latitude:latLng.lat};
                resolve(points);
            }else{
                // 百度地图API功能
                var map = new BMap.Map("allmap");
                var point = new BMap.Point(116.331398,39.897445);
                map.centerAndZoom(point,12);

                var geolocation = new BMap.Geolocation();
                geolocation.getCurrentPosition(function(r){
                    if(this.getStatus() == BMAP_STATUS_SUCCESS){
                        var mk = new BMap.Marker(r.point);
                        map.addOverlay(mk);
                        map.panTo(r.point);

                        let points = {longitude:r.point.lng,latitude:r.point.lat};
                        resolve(points);
                    }
                    else {
                        reject(this.getStatus());
                        console.log('failed'+this.getStatus());
                    }
                },{enableHighAccuracy: true})
            }
            
        });
    },

    //url验签
    checkUrlData(store,data){
        return util.axiosPost('/service/consume/AppcheckDataServlet.do',data);
    },

    //首页数据加载
    loadIndexData(store,data){
        store.commit('setImgBaseUrl',process.env.NODE_ENV);//process.env.NODE_ENV获取配置文件是dev还是build  值分别为development和production

        return util.axiosPost('/service/consume/NavigationBusinessServlet.do',data);
    },

    loadClassificationData(store,data){
        util.axiosPost('/service/consume/CategoryListServlet.do',data).then(resp=>{
            store.commit('setClassificationData',resp.data);
        }).catch(error=>{
            console.log(error);
        });
    }
}

const getters = {
    getYanQianParam(state){
        state.yanQianParam = sessionStorage.getItem('yanQianParam');
        return state.yanQianParam;//请求的时候的验签参数
    },
    getYanQian(state){
        state.yanQian = sessionStorage.getItem('yanQianId');
        return state.yanQian;
    },
    getHotMap(state){
        return state.HotMap;
    },
    getNavigationMap(state){
        return state.NavigationMap;
    },
    getADVLST(state){
        return state.ADVLST;
    },
    getImgBaseUrl(state){
        return state.imgBaseUrl;
    },
    getEtsUrl(state){
        return state.etsUrl;
    },
    getCitysJSON(state){
        return state.citysJSON;
    },
    getSelectedCity(state){
        return state.citysJSON.selected;
    },
    getLongitude(state){
        //state.longitude = sessionStorage.getItem('longitude');//经度
        return state.longitude;
    },
    getLatitude(state){
        //state.latitude = sessionStorage.getItem('latitude');//纬度
        return state.latitude;
    },
    getClassificationMap(state){
        return state.classificationMap;
    }
}

export default ({
    state,
    getters,
    actions,
    mutations
})
